package com.imddysc.jzknavigator.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.imddysc.jzknavigator.service.ZknavigatorService;

@Controller
public class ZknavigatorCreatesubController {
	
	private static final Logger logger = LoggerFactory.getLogger(ZknavigatorCreatesubController.class);
	
	@Autowired
	private ZknavigatorService zknavigatorService;

	@RequestMapping(value = "/createsub", method=RequestMethod.POST)
	public ModelAndView createsub(String node, String content, String path) {
		ModelAndView mav = new ModelAndView();
		logger.info("createsub: ");
		logger.info("node: " + node);
		logger.info("content: " + content);
		logger.info("path: " + path);
		
		String path1 = path.replace("|","/");
		String path2 = path1.replace("//","/");
		String path3;
		if(path2.equals("/")) {
			path3 = "/" + node;
		} else {
			path3 = path2 + "/" + node;
		}
		try {
			zknavigatorService.createAndPutData(path3, content);
			mav.addObject("message", "创建节点成功，保存节点数据成功！");
		} catch (Exception e) {
			mav.addObject("message", "在创建数据节点，保存数据过程中出现异常！");
			logger.error("在创建数据节点，保存数据过程中出现异常！");
			e.printStackTrace();
		}
		
		mav.addObject("last_url", path);
		mav.setViewName("createsub");
		return mav;
	}

}
